package org.mushroomdb.security.parser;


import org.mushroomdb.parser.Parser;
import org.mushroomdb.parser.ParserException;
import org.mushroomdb.query.Query;
import org.mushroomdb.security.queries.AlterUserQuery;
import org.mushroomdb.security.queries.CreateUserQuery;
import org.mushroomdb.security.queries.DropUserQuery;
import org.mushroomdb.security.queries.GrantQuery;
import org.mushroomdb.security.queries.RevokeQuery;

/**
 * @author Tomas
 * 
 * Parses the Security Query. This Class must be deprecated and replaced by a
 * jacc-class parser.
 */
public class SecurityParser implements Parser {

  

    public Query parseQuery(String query) throws ParserException {

        String queryString = format(query);
        Query sQuery = null;

        if (queryString.toUpperCase().startsWith("CREATE USER")) { //$NON-NLS-1$//$NON-NLS-2$
            sQuery = parseCreateUser(queryString);
        } else if (queryString.toUpperCase().startsWith("ALTER USER")) {
            sQuery = parseAlterUser(queryString);
        } else if (queryString.toUpperCase().startsWith("DROP USER")) {
            sQuery = parseDropUser(queryString);
        } else if (queryString.toUpperCase().startsWith("GRANT")) {
            sQuery = parseGrant(queryString);
        } else if (queryString.toUpperCase().startsWith("REVOKE")) {
            sQuery = parseRevoke(queryString);
        }
        return sQuery;
    }

    /**
     * @param queryString
     * @return
     */
    private Query parseRevoke(String queryString) {
        RevokeQuery query = new RevokeQuery(queryString);
        return query;
    }

    /**
     * @param queryString
     * @return
     */
    private Query parseGrant(String queryString) {
        GrantQuery query = new GrantQuery(queryString);
        return query;
    }

    /**
     * @param queryString
     * @return
     */
    private Query parseDropUser(String queryString) {
        DropUserQuery query = new DropUserQuery(queryString);
        return query;
    }

    /**
     * @param queryString
     * @return
     */
    private Query parseAlterUser(String queryString) {
        AlterUserQuery query = new AlterUserQuery(queryString);
        return query;
    }

    /**
     * @param queryString
     * @return
     */
    private Query parseCreateUser(String queryString) {
        CreateUserQuery query = new CreateUserQuery(queryString);
        return query;
    }

    /**
     * @param query
     * Formats query to simplify the parsing
     * @return
     */
    private String format(String query) {
        String result;

    	query = query.replaceAll("\n"," ");
		query = query.replaceAll("\t"," ");
		
        result = query.replaceFirst("create", "CREATE");
        result = query.replaceFirst("user", "USER");
        result = query.replaceFirst("set", "SET");
        result = query.replaceFirst("password", "PASSWORD");
        result = query.replaceFirst("alter", "ALTER");
        result = query.replaceFirst("drop", "DROP");
        result = query.replaceFirst("grant", "GRANT");
        result = query.replaceFirst("on", "ON");
        result = query.replaceFirst("to", "TO");
        result = query.replaceFirst("revoke", "REVOKE");
        result = query.replaceFirst("from", "FROM");
        if (result.indexOf(';') == -1) {
            result = result + ";"; //$NON-NLS-1$
        }

        return result;
    }

    public boolean accepts(String query) {
        String queryString = format(query);

        if (queryString.toUpperCase().startsWith("CREATE USER")) { //$NON-NLS-1$//$NON-NLS-2$
            return true;
        } else if (queryString.toUpperCase().startsWith("ALTER USER")) {
            return true;
        } else if (queryString.toUpperCase().startsWith("DROP USER")) {
            return true;
        } else if (queryString.toUpperCase().startsWith("GRANT")) {
            return true;
        } else if (queryString.toUpperCase().startsWith("REVOKE")) {
            return true;
        }
        return false;

    }

}
